package com.example.timmy.myapplication.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.utils.MyAnimationDrawable;

public class FrameAnimationActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = FrameAnimationActivity.class.getSimpleName();

    private ImageView mIvAnimation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_animation);

        initView();
    }

    private void initView() {
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_pause).setOnClickListener(this);
        mIvAnimation = (ImageView) findViewById(R.id.iv_animation);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                // 开始
                startAnimation();
                break;
            case R.id.btn_pause:
                stopAnimation();
                break;
        }
    }

    private void startAnimation() {
        mIvAnimation.setVisibility(View.VISIBLE);
        MyAnimationDrawable.animateRawManuallyFromXML(R.drawable.heart_too_height, mIvAnimation, null, new Runnable() {
            @Override
            public void run() {
                // 动画结束
                if (mIvAnimation.getVisibility() == View.VISIBLE) {
                    startAnimation();
                }
            }
        });
    }

    private void stopAnimation() {
        mIvAnimation.setVisibility(View.GONE);
    }
}
